class Solution {
public:
    int lengthOfLastWord(string s) {
        int n=s.length()-1,res=0;
        while(n>=0){
            if(s[n]==' '&&res!=0){break;}
            else if(s[n]!=' '){res++;}
            n--;
        }
        return res;
    }
};